package com.yixing.tech.common.hikservice.rcs;

import com.alibaba.fastjson.JSONObject;
import com.yixing.tech.common.base.BusinessException;
import com.yixing.tech.common.utils.HttpClientUtil;
import com.yixing.tech.common.utils.HttpResult;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.http.util.Asserts;

import java.util.UUID;

public class RCSUtil {

    public static void validateRequestParams(RCSTaskRequest request) throws IllegalArgumentException {
        Asserts.check(request != null, "请求参数为空");
        Asserts.check(RCSConfig.getInstance() != null, "RCS配置为空");
        Asserts.check(RCSConfig.getInstance().getIp() != null, "RCS配置参数[ip]为空");
        Asserts.check(RCSConfig.getInstance().getPort() != null, "RCS配置参数[端口]为空");
    }

    public static HttpResult sendRequest(RCSTaskRequest request, String uri) {
        String url = "http://" + RCSConfig.getInstance().getIp() + ":" + RCSConfig.getInstance().getPort() + uri;
        String jsonData = JSONObject.toJSONString(request);
        return HttpClientUtil.getInstance().postJson(url, jsonData);
    }

    /**
     * 请求AGV任务
     * */
    public static RCSResponse genAgvTask(RCSTaskRequest request) throws BusinessException {
        RCSResponse response = new RCSResponse("-1", "请求失败", "");

        validateRequestParams(request);

        if(ObjectUtils.isEmpty(request.getReqCode())) {     // 如果reqCode为空，默认填充32位uuid
            request.setReqCode(UUID.randomUUID().toString().replace("-", ""));
        }
        if(ObjectUtils.isEmpty(request.getTaskCode())) {    // 如果taskCode为空，默认填充32位uuid
            request.setTaskCode(UUID.randomUUID().toString().replace("-", ""));
        }

        HttpResult result = sendRequest(request, URI.GEN_TASK);

        if(result.getStatus() != -1) {
            response = JSONObject.parseObject(result.getBody(), RCSResponse.class);
        }
        return response;
    }

    /**
     * 请求预调度任务
     * */
    public static RCSResponse genPreTask(RCSTaskRequest request) throws BusinessException {
        RCSResponse response = new RCSResponse("-1", "请求失败", "");

        validateRequestParams(request);
        Asserts.check(ObjectUtils.isNotEmpty(request.getPositionCode()), "请求参数[positionCode]为空");
        Asserts.check(ObjectUtils.isNotEmpty(request.getNextTask()), "请求参数[nextTask]为空");

        if(ObjectUtils.isEmpty(request.getReqCode())) {     // 如果reqCode为空，默认填充32位uuid
            request.setReqCode(UUID.randomUUID().toString().replace("-", ""));
        }
        if(ObjectUtils.isEmpty(request.getTaskCode())) {    // 如果taskCode为空，默认填充32位uuid
            request.setTaskCode(UUID.randomUUID().toString().replace("-", ""));
        }

        HttpResult result = sendRequest(request, URI.GEN_PRE_TASK);

        if(result.getStatus() != -1) {
            response = JSONObject.parseObject(result.getBody(), RCSResponse.class);
        }
        return response;
    }

    /**
     * 请求继续执行任务
     * */
    public static RCSResponse continueAgvTask(RCSTaskRequest request) throws BusinessException {
        RCSResponse response = new RCSResponse("-1", "请求失败", "");

        validateRequestParams(request);
        Asserts.check(request.getTaskCode() != null, "请求参数[任务号]为空");

        if(ObjectUtils.isEmpty(request.getReqCode())) {     // 如果reqCode为空，默认填充32位uuid
            request.setReqCode(UUID.randomUUID().toString().replace("-", ""));
        }

        HttpResult result = sendRequest(request, URI.CONTINUE_TASK);

        if(result.getStatus() != -1) {
            response = JSONObject.parseObject(result.getBody(), RCSResponse.class);
        }
        return response;
    }

    /**
     * 请求取消任务
     * */
    public static RCSResponse cancelAgvTask(RCSTaskRequest request) throws BusinessException {
        RCSResponse response = new RCSResponse("-1", "请求失败", "");

        validateRequestParams(request);
        Asserts.check(request.getTaskCode() != null, "请求参数[任务号]为空");

        if(ObjectUtils.isEmpty(request.getReqCode())) {     // 如果reqCode为空，默认填充32位uuid
            request.setReqCode(UUID.randomUUID().toString().replace("-", ""));
        }

        HttpResult result = sendRequest(request, URI.CANCEL_TASK);

        if(result.getStatus() != -1) {
            response = JSONObject.parseObject(result.getBody(), RCSResponse.class);
        }
        return response;
    }

    /** 请求暂停机器人 */
    public static RCSResponse stopRobot(RCSTaskRequest request) throws BusinessException {
        RCSResponse response = new RCSResponse("-1", "请求失败", "");

        validateRequestParams(request);
        if("-1".equals(request.getRobotCount())) {
            Asserts.check(request.getMapShortName() != null, "请求参数[地图简称]为空");
        }

        if(ObjectUtils.isEmpty(request.getReqCode())) {     // 如果reqCode为空，默认填充32位uuid
            request.setReqCode(UUID.randomUUID().toString().replace("-", ""));
        }

        HttpResult result = sendRequest(request, URI.STOP_ROBOT);

        if(result.getStatus() != -1) {
            response = JSONObject.parseObject(result.getBody(), RCSResponse.class);
        }
        return response;
    }

    /** 请求恢复机器人 */
    public static RCSResponse resumeRobot(RCSTaskRequest request) throws BusinessException {
        RCSResponse response = new RCSResponse("-1", "请求失败", "");

        validateRequestParams(request);
        if("-1".equals(request.getRobotCount())) {
            Asserts.check(request.getMapShortName() != null, "请求参数[地图简称]为空");
        }

        if(ObjectUtils.isEmpty(request.getReqCode())) {     // 如果reqCode为空，默认填充32位uuid
            request.setReqCode(UUID.randomUUID().toString().replace("-", ""));
        }

        HttpResult result = sendRequest(request, URI.RESUME_ROBOT);

        if(result.getStatus() != -1) {
            response = JSONObject.parseObject(result.getBody(), RCSResponse.class);
        }
        return response;
    }

    /** 绑定库位和货架 */
    public static RCSResponse bindPodAndBerth(RCSTaskRequest request) throws BusinessException {
        RCSResponse response = new RCSResponse("-1", "请求失败", "");

        validateRequestParams(request);
        Asserts.check(request.getPodCode() != null, "请求参数[货架编号]为空");
        Asserts.check(request.getPositionCode() != null, "请求参数[位置编号]为空");
        Asserts.check(request.getIndBind() != null, "请求参数[绑定/解绑]为空");

        if(ObjectUtils.isEmpty(request.getReqCode())) {     // 如果reqCode为空，默认填充32位uuid
            request.setReqCode(UUID.randomUUID().toString().replace("-", ""));
        }

        HttpResult result = sendRequest(request, URI.BIND_POD);

        if(result.getStatus() != -1) {
            response = JSONObject.parseObject(result.getBody(), RCSResponse.class);
        }
        return response;
    }

    /** 料箱取放回调
     * @param request
     * {
     *     "reqCode": "请求编号",
     *     "reqTime": "请求时间戳",
     *     "clientCode": "客户端编号",
     *     "tokenCode": "令牌号",
     *     "taskCode": "任务单号",
     *     "type": "1取申请通过 2放申请通过"
     * }
     * */
    public static RCSResponse boxApplyPass(RCSTaskRequest request) throws BusinessException {
        RCSResponse response = new RCSResponse("-1", "请求失败", "");

        validateRequestParams(request);
        Asserts.check(request.getTaskCode() != null && !"".equals(request.getTaskCode()), "请求参数[任务编号]为空");
        Asserts.check(request.getType() != null && !"".equals(request.getType()), "请求参数[申请通过]为空");

        if(ObjectUtils.isEmpty(request.getReqCode())) {     // 如果reqCode为空，默认填充32位uuid
            request.setReqCode(UUID.randomUUID().toString().replace("-", ""));
        }

        HttpResult result = sendRequest(request, URI.BOX_APPLY_PASS);

        if(result.getStatus() != -1) {
            response = JSONObject.parseObject(result.getBody(), RCSResponse.class);
        }
        return response;
    }
}
